package com.football.springboot.controller;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.football.springboot.common.Constants;
import com.football.springboot.common.Result;
import com.football.springboot.entity.Files;
import com.football.springboot.entity.User;
import com.football.springboot.vo.RoleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.football.springboot.common.Return;

//导入service
import com.football.springboot.service.IRoleService;
//导入entity
import com.football.springboot.entity.Role;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 李越
 * @since 2023-02-13
 */
@Api(tags = "角色管理")
@RestController
@RequestMapping("/role")
public class RoleController {

    @Resource
    private IRoleService roleService;

    //新增或者更新
    @ApiOperation("新增或者更新角色")
    @RequestMapping(value = "/saveOrUpdate",method = RequestMethod.POST)
    public Return save(@RequestBody Role role) {
        String code = "";
        Boolean flag = roleService.saveOrUpdate(role);
        if (flag){
            code= Constants.CODE_200;
        }else{
            code="";
        }
        return Return.ok().put("code",code);
    }


    //删除
    @ApiOperation("删除")
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.DELETE)
    public Return delete(@PathVariable Integer id) {
        String code = "";
        Boolean isdelete = roleService.removeById(id);
        if (isdelete){
            System.out.println("删除成功");
            code= Constants.CODE_200;
        }else{
            System.out.println("删除失败");
            code="";
        }
        return Return.ok().put("code",code);
    }

    //批量删除
    @ApiOperation("批量删除")
    @RequestMapping(value = "/delete/batch",method = RequestMethod.POST)
    public Return deleteBatch(@RequestBody List<Integer> ids) {
        String code = "";
        Boolean isdelete = roleService.removeByIds(ids);
        if (isdelete){
            code= Constants.CODE_200;
        }else{
            code="";
        }

        return Return.ok().put("code",code);
    }

    //查询全部
    @ApiOperation("查询全部")
    @RequestMapping(value = "/findall",method = RequestMethod.GET)
    public Return findAll() {
        return Return.ok().put("data",(roleService.list()));
    }

    //获取单个角色信息
    @ApiOperation("获取单个角色信息")
    @RequestMapping(value = "/getRole/{id}",method = RequestMethod.GET)
    public Return findOne(@PathVariable Integer id) {
        return Return.ok().put("data",(roleService.getById(id)));
    }


    //分页查询
    @ApiOperation("分页查询")
    @RequestMapping(value = "/pageRole",method = RequestMethod.GET)
    public Return pageRole(@RequestParam Integer current,
                           @RequestParam Integer limit,
                           RoleQueryVo roleQueryVo) {
        //创建page对象,传递当前页数current,每页记录数limit
        Page<Role> page = new Page<>(current,limit);

        //构建条件 模糊查询
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();

        String name = roleQueryVo.getName();

        //角色名查询
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like("name", name);
        }

        queryWrapper.orderByDesc("id");
        Page<Role> pageRole = roleService.page(page,queryWrapper);
        return Return.ok().put("pageRole",pageRole).put("total",pageRole.getTotal());
    }


    //绑定角色和菜单的关系
    @ApiOperation("绑定角色和菜单的关系")
    @PostMapping("/roleMenu/{roleId}")
    public Result roleMenu(@PathVariable Integer roleId, @RequestBody List<Integer> menuIds) {
        roleService.setRoleMenu(roleId, menuIds);
        return Result.success();
    }

    //查看该角色绑定的菜单 根据角色id查找菜单id列表
    @ApiOperation("查看该角色绑定的菜单 根据角色id查找菜单id列表")
    @GetMapping("/roleMenu/{roleId}")
    public Result getRoleMenu(@PathVariable Integer roleId) {

        return Result.success( roleService.getRoleMenu(roleId));
    }

}

